package windowFactoryPackage;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class AccountPopUp extends JFrame {
	Insert ins;

	// Panel p1
	JLabel jlblUser_ID;
	JLabel jlblUserPass;
	JLabel jlblRetypePass;
	JLabel jlblUserEmail;
	JLabel jlblUserAddr;
	JLabel jlblUserStreet;
	JLabel jlblUserCityStZip;
	JLabel jlblUserPhone;
	JLabel jlblCreditCardNum, jlblCreditExpDate, jlblCreditHyphen;
	JPanel creditPanel;

	JLabel spacer;
	
	//First and Last user name
	JLabel jlblUserFirstName;
	static JTextField jtfUserFirstName;
	JLabel jlblUserLastName;
	static JTextField jtfUserLastName;

	static JTextField jtfUser_ID;
	static JPasswordField jpfUserPassword;
	static JPasswordField jpfRetypePass;
	static JTextField jtfUserEmail;
	
	static JTextField jtfUserStreet;
	
	// __________  | __ | _____ City/ST/Zip
	JLabel jlblCitySTZip;
	static JTextField jtfUserCity;
	static JTextField jtfUserST;
	static JTextField jtfUserZip;
	
	// ( ___ ) ___ - ____
	JLabel jlblPhoneOpenParen;
	static JTextField jtfPhoneArea;
	JLabel jlblPhoneCloseParen;
	static JTextField jtfPhonePrefix;
	JLabel jlblPhoneDash;
	static JTextField jtfPhoneLine;
	
	static JTextField jtfCreditCardNum, jtfCreditExpMonth, jtfCreditExpYear;

	JButton create;

	public AccountPopUp() {
		setTitle("Create Account");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(500, 350);
		setResizable(false);
		
		spacer = new JLabel("");

		jlblUser_ID = new JLabel("* User Name: ");
		jtfUser_ID = new JTextField("", 5);
		jtfUser_ID.setToolTipText("Enter 3-20 characters.");
		
		jlblUserPass = new JLabel("* Account Password: ");
		jpfUserPassword = new JPasswordField();
		jpfUserPassword.setToolTipText("Enter 6-20 characters");
		jlblRetypePass = new JLabel("* Retype Password: ");
		jpfRetypePass = new JPasswordField();
		jpfRetypePass.setToolTipText("Retype password from above.");
		
		jlblUserFirstName = new JLabel("Enter First Name: ");
		jtfUserFirstName = new JTextField("", 5);
		jlblUserLastName = new JLabel("Enter Last Name: ");
		jtfUserLastName = new JTextField("", 5);

		jlblUserEmail = new JLabel("* Account Email: ");
		jtfUserEmail = new JTextField("", 5);
		jtfUserEmail.setToolTipText("Enter valid Email Address.");
		
		jlblUserAddr = new JLabel("Shipping Address: ");
		jlblUserStreet = new JLabel("     * Street: ");
		jtfUserStreet = new JTextField("", 5);
		jtfUserStreet.setToolTipText("Enter street name.");
		jlblCitySTZip = new JLabel("     * City | ST | Zip: ");
		jtfUserCity = new JTextField("", 7);
		jtfUserCity.setToolTipText("Enter city name.");
		jtfUserST = new JTextField("", 2);
		jtfUserST.setToolTipText("Enter 2 character state code (eg. GA).");
		jtfUserZip = new JTextField("", 5);
		jtfUserZip.setToolTipText("Enter 5-digit zip code.");
		
		jlblUserPhone = new JLabel("Phone Number: ");
		jlblPhoneOpenParen = new JLabel("(");
		jtfPhoneArea = new JTextField("",3);
		jtfPhoneArea.setToolTipText("Enter 3-digit Area Code.");
		jlblPhoneCloseParen = new JLabel(") ");
		jtfPhonePrefix = new JTextField("",3);
		jtfPhonePrefix.setToolTipText("Enter 3-ditig prefix.");
		jlblPhoneDash = new JLabel(" - ");
		jtfPhoneLine = new JTextField("",4);
		jtfPhoneLine.setToolTipText("Enter 4-digit line number.");
		
		jlblCreditCardNum = new JLabel("Credit Card: ");
		jlblCreditExpDate = new JLabel("Exp: ");
		jlblCreditHyphen = new JLabel("-");
		jtfCreditCardNum = new JTextField(20);
		jtfCreditExpMonth = new JTextField("MMM", 3);
		jtfCreditExpYear = new JTextField("YYYY", 4);
		

		//phone# panel
		JPanel pPhone = new JPanel();
		pPhone.setLayout(new FlowLayout());
		pPhone.add(jlblPhoneOpenParen);
		pPhone.add(jtfPhoneArea);
		pPhone.add(jlblPhoneCloseParen);
		pPhone.add(jtfPhonePrefix);
		pPhone.add(jlblPhoneDash);
		pPhone.add(jtfPhoneLine);
		
		//city/st/zip panel
		Panel pCitySTZip = new Panel();
		pCitySTZip.setLayout(new FlowLayout());
		
		pCitySTZip.add(jtfUserCity);
		pCitySTZip.add(jtfUserST);
		pCitySTZip.add(jtfUserZip);
		
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(0,1));
		p1.setLayout(new GridLayout(0,1));
		// Account Name
		p2.add(jlblUser_ID);
		p1.add(jtfUser_ID);
		// Account Password
		p2.add(jlblUserPass);
		p1.add(jpfUserPassword);
		// retype Password (only check consistency, before adding to table)
		p2.add(jlblRetypePass);
		p1.add(jpfRetypePass);
		//first and last name
		p2.add(jlblUserFirstName);
		p1.add(jtfUserFirstName);
		p2.add(jlblUserLastName);
		p1.add(jtfUserLastName);
		// Account Email
		p2.add(jlblUserEmail);
		p1.add(jtfUserEmail);
		// Account Address
		p2.add(jlblUserAddr);
		p1.add(spacer);
		p2.add(jlblUserStreet);
		p1.add(jtfUserStreet);
		p2.add(jlblCitySTZip);
		p1.add(pCitySTZip);
		// Account Phone (null allowed)
		p2.add(jlblUserPhone);
		p1.add(pPhone);
		p2.add(jlblCreditCardNum);
		p1.add(jtfCreditCardNum);
		
		creditPanel = new JPanel();
		creditPanel.add(jtfCreditCardNum);
		creditPanel.add(jlblCreditExpDate);
		creditPanel.add(jtfCreditExpMonth);
		jtfCreditExpMonth.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent arg0) {
				jtfCreditExpMonth.setText("");				
			}
			@Override
			public void focusLost(FocusEvent arg0) {}			
		});
		creditPanel.add(jtfCreditExpYear);
		jtfCreditExpYear.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {
				jtfCreditExpYear.setText("");
			}
			@Override
			public void focusLost(FocusEvent e) {}
		});		
		p1.add(creditPanel);
		

		JPanel buttonPanel = new JPanel();
		create = new JButton("Create Account");
		buttonPanel.add(create);

		add(p1, BorderLayout.CENTER);
		add(p2, BorderLayout.WEST);
		add(buttonPanel, BorderLayout.SOUTH);

		// CREATE PopUp User Creation
		create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean flag;
				ins = new Insert();
				flag = ins.createUser();
				if(flag == true)
					dispose();
			}
		});

	}
}